<?php 

class App_Form_Decorator_FCKEditor extends Zend_Form_Decorator_Abstract
{
    /**
     * Получение строк подключения Javacript и CSS для календаря
     * Статическая переменная $jsAndCss отвечает за то, чтобу подключение
     * осуществлялось только один раз
     *
     * @return string
     */
    private function _getJsAndCss()
    {
        static $jsAndCss = null;
        
        if($jsAndCss === null) {

            $jsAndCss = '
            <style type="text/css">@import url(/js/sample.css);</style>
            <script type="text/javascript" src="/js/fckeditor.js"></script>
            ';
            
            return $jsAndCss;
        }
        return '';
    }
    
    
    /**
     * Получение кода ссылки и изображения каледаря. Настройка календаря
     *
     * @return string
     */
    private function _getCalendarLink()
    {
		$sBasePath = "/js/" ;
		//$sBasePath = substr( $sBasePath, 0, strpos( $sBasePath, "_samples" ) ) ;
		
		$oFCKeditor = new App_FCKeditor($this->getElement()->getName()) ;
		$oFCKeditor->BasePath	= $sBasePath ;
		$oFCKeditor->Height = 300;
		$oFCKeditor->Width = 800;
		$oFCKeditor->Value		= $this->getElement()->getValue();//'This is some <strong>sample text</strong>. You are using <a href="http://www.fckeditor.net/">FCKeditor</a>.' ;
		$oFCKeditor->Create() ;		
        
        return $oFCKeditor->Create() ;
    }
    
	public function buildLabel()
    {
        $element = $this->getElement();
        $label = $element->getLabel();
        if ($translator = $element->getTranslator()) {
            $label = $translator->translate($label);
        }
        if ($element->isRequired()) {
            $label .= ' <span style="color: red">*</span>';
        }
        $label .= ':';
        return $label;
    }    

	public function buildInput()
    {
        $element = $this->getElement();
        $helper  = $element->helper;
        return $element->getView()->$helper(
            $element->getName(),
            $element->getValue(),
            $element->getAttribs(),
            $element->options
        );
    }

    public function buildErrors()
    {
        $element  = $this->getElement();
        $messages = $element->getMessages();
        if (empty($messages)) {
            return '';
        }
        return '<div class="errors">' .
               $element->getView()->formErrors($messages) . '</div>';
    }

    public function buildDescription()
    {
        $element = $this->getElement();
        $desc    = $element->getDescription();
        if (empty($desc)) {
            return '';
        }
        return '<div class="description">' . $desc . '</div>';
    }

	public function render($content)
    {
        $element = $this->getElement();
        if (!$element instanceof Zend_Form_Element) {
            return $content;
        }
        if (null === $element->getView()) {
            return $content;
        }

        $separator = $this->getSeparator();
        $placement = $this->getPlacement();
        $label     = $this->buildLabel();
        $errors    = $this->buildErrors();
        $desc      = $this->buildDescription();

        $output = '<tr><td>'
                . $label. '</td><td>'
                . $this->_getJsAndCss() . $this->_getCalendarLink()
                . $errors
                . $desc
                . '</td></tr>';
        return  $output;
    }
}

?>